package wqh.utils;

import org.apache.poi.hssf.usermodel.*;
import wqh.pojo.MonitorXls;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by devts on 2016/9/29.
 */
public class XlsUtil {

    private String columnNames[];

    public XlsUtil()
    {

    }

    public XlsUtil(String[] columnNames) {
        this.columnNames = columnNames;
    }


    public Map<String , Object> readXlsFile(InputStream inputStream) throws IOException
    {
        List<MonitorXls> monitorXlsList = new ArrayList<MonitorXls>();
        Map<String, Object> errorCode = new HashMap<String, Object>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Object> errorList = new ArrayList<Object>();
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);

        HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
        if(hssfSheet == null || hssfSheet.getLastRowNum() == 0)
        {
            errorCode.put("code",-3);
            errorCode.put("error","工作表为空。");
            resultMap.put("data",null);
            resultMap.put("error",errorCode);
            return resultMap;
        }
        for(int row = 2 ; row <= hssfSheet.getLastRowNum(); row++)
        {
            MonitorXls monitorXls = new MonitorXls();
            HSSFRow hssfRow = hssfSheet.getRow(row);
            if(hssfRow.getLastCellNum() < 8 )
            {
                System.out.println(hssfRow.getLastCellNum());
                errorCode.put("code",-3);
                errorCode.put("error","工作表有"+(hssfSheet.getLastRowNum() - 1)+"条数据，工作表第 " + row + 1 + " 行不符合格式要求。");
                resultMap.put("data",null);
                resultMap.put("error",errorCode);
                return resultMap;
            }
            for(int i = 0; i < hssfRow.getLastCellNum(); i++)
            {
                HSSFCell cell = hssfRow.getCell(i);
                if(cell == null || cell.toString().equals(""))
                {
                    Map<String,Object> errorCell = new HashMap<String, Object>();
                    errorCell.put("错误行",row + 1);
                    errorCell.put("错误列",i + 1);
                    errorList.add(errorCell);
                    continue;
                }
                if(i == 0) monitorXls.setDevice(cell.toString());
                if(i == 1) monitorXls.setDevice_ip(cell.toString());
                if(i == 2) monitorXls.setArea(cell.toString());
                if(i == 3) monitorXls.setBuilding(cell.toString());
                if(i == 4) monitorXls.setFloor(cell.toString());
                if(i == 5) monitorXls.setSensor(cell.toString());
                if(i == 6) monitorXls.setMonitor(cell.toString());
                if(i == 7) monitorXls.setAlarm((cell.toString()).substring(0,cell.toString().lastIndexOf(".")));
            }
            monitorXlsList.add(monitorXls);
        }
        resultMap.put("data",monitorXlsList);
        if(errorList.size() == 0)
        {
            resultMap.put("error",null);
        }
        else
        {
            errorCode.put("cellError",errorList);
            resultMap.put("error",errorCode);
        }
        return resultMap;
    }
}
